package com.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import com.dao.AccountDaoImpl;
import com.dao.CourseDaoImpl;
import com.dao.DepartmentDaoImpl;
import com.dao.FileDaoImpl;
import com.dao.MajorDaoImpl;
import com.dao.ScoreDaoImpl;
import com.dao.StudentDaoImpl;
import com.dao.TeacherDaoImpl;
import com.entity.AccountEntity;
import com.entity.CourseEntity;
import com.entity.DepartmentEntity;
import com.entity.FileEntity;
import com.entity.LoginEntity;
import com.entity.PyqEntity;
import com.entity.ScoreEntity;
import com.entity.StudentEntity;
import com.entity.TeacherEntity;
import com.entity.majorEntity;

import javassist.compiler.ast.NewExpr;

public class spaceService {
	public StudentEntity getStudentEntity(LoginEntity loginEntity) {
		StudentEntity studentEntity = new StudentEntity();
		// TODO: 通过loginEntity中的useraccount获取对应的学生信息,并将学生信息赋值给studentEntity
		// Return : studentEntity
		
		WebApplicationContext ctx=ContextLoader.getCurrentWebApplicationContext();
		//ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		String str;
		StudentDaoImpl studentDao = (StudentDaoImpl)ctx.getBean("studentDao");
		List <StudentEntity> student=new ArrayList<StudentEntity>();
		
		str="and studentNo="+"'"+loginEntity.getUseraccount()+"'";
		student=studentDao.findByCondition(str,null,null,true);
		
		studentEntity.setStudentNo(student.get(0).getStudentNo());
		studentEntity.setStudentName(student.get(0).getStudentName());
		studentEntity.setStudentMajor(student.get(0).getStudentMajor());
		studentEntity.setStudentGrade(student.get(0).getStudentGrade());
		studentEntity.setStudentDepartment(student.get(0).getStudentDepartment());
		
		return studentEntity;
	}
	
	public List<ScoreEntity> getStudentScoreEntities(LoginEntity loginEntity) {
		List<ScoreEntity> studentScoreEntities = new ArrayList<ScoreEntity>();
		// TODO: 通过loginEntity中的useraccount获取对应的学生成绩,并将学生成绩赋值给studentScoreEntities
		// Return : studentScoreEntities
		
		WebApplicationContext ctx=ContextLoader.getCurrentWebApplicationContext();
		//ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		String str;
		ScoreDaoImpl scoreDao = (ScoreDaoImpl)ctx.getBean("scoreDao");
		
		str="and studentNo="+"'"+loginEntity.getUseraccount()+"'";
		studentScoreEntities=scoreDao.findByCondition(str,null,null,true);
		
		return studentScoreEntities;
	}
	
	public CourseEntity getCourseByScoreEntities(ScoreEntity scoreEntity) {
		CourseEntity courseEntity = new CourseEntity();
		// TODO: 通过scoreEntity获取对应的课程的详细信息,并将课程信息赋值给courseEntity
		// Return : courseEntity
		
		WebApplicationContext ctx=ContextLoader.getCurrentWebApplicationContext();
		//ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		String str;
		CourseDaoImpl courseDao = (CourseDaoImpl)ctx.getBean("courseDao");
		List <CourseEntity> course=new ArrayList<CourseEntity>();
		
		str="and courseNo="+"'"+scoreEntity.getCourseNo()+"'";
		course=courseDao.findByCondition(str,null,null,true);
		
		courseEntity.setCourseInfo(course.get(0).getCourseInfo());
		courseEntity.setCourseName(course.get(0).getCourseName());
		courseEntity.setCourseNo(course.get(0).getCourseNo());
		courseEntity.setIndex(course.get(0).getIndex());
		courseEntity.setMajorNo(course.get(0).getMajorNo());
		courseEntity.setSelect_enable(course.get(0).getSelect_enable());
		courseEntity.setTeacherNo(course.get(0).getTeacherNo());

		return courseEntity;
	}
	public List<CourseEntity> getSelectableCourseByStudentEntities(StudentEntity studentEntity) {
		List<CourseEntity> selectableCourseEntities = new ArrayList<CourseEntity>();
		// TODO: 通过studentEntity中的学生专业信息查找获取对应专业开设的所有课程,并将课程List赋值给selectableCourseEntities
		// 除了获取开设的课程信息，还要注意的是课程信息中有一个属性的select_enable,要判断该值来确定是否可选
		// Return : selectableCourseEntities
		
		WebApplicationContext ctx=ContextLoader.getCurrentWebApplicationContext();
		//ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		String str;
		CourseDaoImpl courseDao = (CourseDaoImpl)ctx.getBean("courseDao");
		
		str="and majorNo="+"'"+studentEntity.getStudentMajor()+"'"+" and enableSelect='true'";
		selectableCourseEntities=courseDao.findByCondition(str,null,null,true);
		
		return selectableCourseEntities;
	}
	
	public TeacherEntity getTeacherEntity(LoginEntity loginEntity) {
		TeacherEntity teacherEntity = new TeacherEntity();
		// TODO: 通过loginEntity中的useraccount获取对应的教师信息,并将教师信息赋值给teacherEntity
		// Return : teacherEntity
		WebApplicationContext ctx=ContextLoader.getCurrentWebApplicationContext();
		//ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		String str;
		TeacherDaoImpl teacherDao = (TeacherDaoImpl)ctx.getBean("teacherDao");
		List <TeacherEntity> teacher=new ArrayList<TeacherEntity>();
		
		str="and teacherNo="+"'"+loginEntity.getUseraccount()+"'";
		teacher=teacherDao.findByCondition(str,null,null,true);
		
		teacherEntity.setTeacherNo(teacher.get(0).getTeacherNo());
		teacherEntity.setTeacherName(teacher.get(0).getTeacherName());
		teacherEntity.setTeacherNo(teacher.get(0).getTeacherNo());

		return teacherEntity;
	}
	public List<CourseEntity> getCourseByTeacherEntities(TeacherEntity teacherEntity) {
		List<CourseEntity> courseEntities = new ArrayList<CourseEntity>();
		// TODO: 通过teacherEntity中的教师No获取对应的课程的详细信息,并将课程信息赋值给courseEntities
		// Return : courseEntities
		WebApplicationContext ctx=ContextLoader.getCurrentWebApplicationContext();
		//ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		String str;
		CourseDaoImpl courseDao = (CourseDaoImpl)ctx.getBean("courseDao");
		
		str="and teacherNo="+"'"+teacherEntity.getTeacherNo()+"'";
		courseEntities=courseDao.findByCondition(str,null,null,true);
		
		return courseEntities;
	}
	
	public List<FileEntity> getFileEntities() {
		List<FileEntity> fileEntities = new ArrayList<FileEntity>();
		// TODO: 获取所有的文件信息，并赋值给fileEntities
		// Return : fileEntities
		WebApplicationContext ctx=ContextLoader.getCurrentWebApplicationContext();
		//ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		FileDaoImpl fileDao = (FileDaoImpl)ctx.getBean("fileDao");
		
		fileEntities=fileDao.findAll(true);
		
		return fileEntities;
	}
	
	public List<DepartmentEntity> getDepartmentEntities() {
		List<DepartmentEntity> departmentEntities = new ArrayList<DepartmentEntity>();
		// TODO: 获取所有的学院信息，并赋值给departmentEntities
		// Return : departmentEntities
		WebApplicationContext ctx=ContextLoader.getCurrentWebApplicationContext();
		//ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		DepartmentDaoImpl departmentDao = (DepartmentDaoImpl)ctx.getBean("departmentDao");
		
		departmentEntities=departmentDao.findAll(true);
		
		return departmentEntities;
	}
	
	public List<majorEntity> getmajorEntities() {
		List<majorEntity> majorEntities = new ArrayList<majorEntity>();
		// TODO: 获取所有的专业信息，并赋值给majorEntities
		// Return : majorEntities
		WebApplicationContext ctx=ContextLoader.getCurrentWebApplicationContext();
		//ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		MajorDaoImpl majorDao = (MajorDaoImpl)ctx.getBean("majorDao");
		
		majorEntities=majorDao.findAll(true);
		
		return majorEntities;
	}
	
	public List<StudentEntity> getStudentEntities() {
		List<StudentEntity> studentEntities = new ArrayList<StudentEntity>();
		// TODO: 获取所有的学生信息，并赋值给studentEntities
		// Return : studentEntities
		WebApplicationContext ctx=ContextLoader.getCurrentWebApplicationContext();
		//ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		StudentDaoImpl studentDao = (StudentDaoImpl)ctx.getBean("studentDao");
				
		studentEntities=studentDao.findAll(true);
				
		return studentEntities;
	}
	
	public List<TeacherEntity> getTeacherEntities() {
		List<TeacherEntity> teacherEntities = new ArrayList<TeacherEntity>();
		// TODO: 获取所有的教师信息，并赋值给teacherEntities
		// Return : teacherEntities
		WebApplicationContext ctx=ContextLoader.getCurrentWebApplicationContext();
		//ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		TeacherDaoImpl teacherDao = (TeacherDaoImpl)ctx.getBean("teacherDao");
				
		teacherEntities=teacherDao.findAll(true);
				
		return teacherEntities;
	}
	
	public List<CourseEntity> getCourseEntities() {
		List<CourseEntity> courseEntities = new ArrayList<CourseEntity>();
		// TODO: 获取所有的课程信息，并赋值给courseEntities
		// Return : courseEntities
		WebApplicationContext ctx=ContextLoader.getCurrentWebApplicationContext();
		//ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		CourseDaoImpl courseDao = (CourseDaoImpl)ctx.getBean("courseDao");
				
		courseEntities=courseDao.findAll(true);
		return courseEntities;
	}
	
	public void uploadFile(FileEntity fileEntity,String fileType,List<File> files) throws FileNotFoundException,IOException{
		try {
			InputStream inputStream = new FileInputStream(files.get(0));
			String storeRootDir = "/upload";
			String realPath = ServletActionContext.getServletContext().getRealPath(storeRootDir);
			File fileLocationFile = new File(realPath);
			if(!fileLocationFile.exists()) {
				boolean isCreated = fileLocationFile.mkdir();
				System.out.println("dir"+storeRootDir+" "+"missing");
				if(!isCreated) {
					System.out.println("create dir faild");
					return ;
				}
			}
			File uploadFile = new File(realPath,fileEntity.getFileName());
			OutputStream out = new FileOutputStream(uploadFile);
			byte[] buffer = new byte[1024*1024];
			int length;
			while((length = inputStream.read(buffer))>0) {
				out.write(buffer,0,length);
			}
			inputStream.close();
			out.close();
			fileEntity.setFileName(fileEntity.getFileName());
			fileEntity.setFileType(fileType);
			fileEntity.setFileLocation(realPath+"/"+fileEntity.getFileName());
			fileEntity.setReleaseTime(new Timestamp(new Date().getTime()));
			
			if(addFileEntitytoTable(fileEntity)) {
				System.out.println("add file to Table successfully");
			}else {
				System.out.println("add file to Table faild");
			}
		} catch (FileNotFoundException ex) {
			System.out.println("上传失败");
			ex.printStackTrace();
			
		} catch (IOException ex) {
			System.out.println("上传失败");
			ex.printStackTrace();
			
		}
	}
	
	public void deleteFile(FileEntity fileEntity) throws IOException{
		try {
			String storeRootDir = "/upload/"+fileEntity.getFileName();
			String realPath = ServletActionContext.getServletContext().getRealPath(storeRootDir);
			Path path = Paths.get(realPath);
			boolean result = Files.deleteIfExists(path);
			System.out.println(result);
		} catch (IOException ex) {
			// TODO: handle exception
			ex.printStackTrace();
		}
			  

	}
	
	public Boolean addFileEntitytoTable(FileEntity fileEntity) {
		// TODO: 将传入的fileEntity存到数据库里面
		//Return : true|false
		WebApplicationContext ctx=ContextLoader.getCurrentWebApplicationContext();
		//ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		FileDaoImpl fileDao = (FileDaoImpl)ctx.getBean("fileDao");
		fileDao.save(fileEntity);

		return true;
	}
	
	public CourseEntity getCourseEntityByCourseNoAndTeacherNo(String courseNo,String teacherNo) {
		CourseEntity courseEntity = new CourseEntity();
		// TODO: 通过传入的课程代码和教师编号，从Course表中找到对应的那个课程，并返回该课程的实例courseEntity
		//Return : courseEntity
		
		WebApplicationContext ctx=ContextLoader.getCurrentWebApplicationContext();
		//ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		String str;
		CourseDaoImpl courseDao = (CourseDaoImpl)ctx.getBean("courseDao");
		List <CourseEntity> course=new ArrayList<CourseEntity>();
		
		str="and courseNo="+"'"+courseNo+"'"+"and teacherNo="+"'"+teacherNo+"'";
		course=courseDao.findByCondition(str,null,null,true);
		
		courseEntity.setCourseInfo(course.get(0).getCourseInfo());
		courseEntity.setCourseName(course.get(0).getCourseName());
		courseEntity.setCourseNo(course.get(0).getCourseNo());
		courseEntity.setIndex(course.get(0).getIndex());
		courseEntity.setMajorNo(course.get(0).getMajorNo());
		courseEntity.setSelect_enable(course.get(0).getSelect_enable());
		courseEntity.setTeacherNo(course.get(0).getTeacherNo());
		
		return courseEntity;
	}
	
	public boolean addScoreToTable(ScoreEntity scoreEntity) {
		// TODO: 通过传入的scoreEntity添加到score表中
		//Return : true | false
//		System.out.println("select course7");
//		WebApplicationContext ctx=ContextLoader.getCurrentWebApplicationContext();
//		//ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
//		System.out.println("select course8");
//		ScoreDaoImpl scoreDao = (ScoreDaoImpl)ctx.getBean("scoreDao");
//		System.out.println("select course9");
//		scoreDao.save(scoreEntity);
//		System.out.println("select course10");
		
		WebApplicationContext ctx=ContextLoader.getCurrentWebApplicationContext();
//		//ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		SessionFactory sessionFactory = (SessionFactory)ctx.getBean("sessionFactory");
		Session session=sessionFactory.openSession();
		Transaction transaction=session.getTransaction();
		transaction.begin();
		String sql="INSERT INTO `javaEE_lab_database`.`scoreTable` (`courseNo`, `courseName`, `teacherNo`, `studentNo`, `score`, `index`) VALUES (?, ?, ?, ?, ?, ?);";
		
		//根据sql创建SQLQuery
		SQLQuery query=session.createSQLQuery(sql).addEntity(ScoreEntity.class);
		query.setString(0, scoreEntity.getCourseNo())
			 .setString(1, scoreEntity.getCourseName())
			 .setString(2, scoreEntity.getTeacherNo())
			 .setString(3, scoreEntity.getStudentNo())
			 .setFloat(4, scoreEntity.getScore())
			 .setString(5, scoreEntity.getIndex());

		//执行新增
		int r=query.executeUpdate();
		if(r>0){
			System.out.println("新增成功");
		}else{
			System.out.println("新增失败");
		}
		transaction.commit();
		session.close();
		sessionFactory.close();
		
		return true;
	}
	
	public boolean updateCourse(CourseEntity courseEntity) {
		// TODO: 通过传入的courseEntity更新到course表中
		//Return : true | false
		
		WebApplicationContext ctx=ContextLoader.getCurrentWebApplicationContext();
//		//ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		SessionFactory sessionFactory = (SessionFactory)ctx.getBean("sessionFactory");
		Session session=sessionFactory.openSession();
		Transaction transaction=session.getTransaction();
		transaction.begin();
		String sql="UPDATE `javaEE_lab_database`.`courseTable` SET `enableSelect` = ? WHERE (`courseNo` = ?);";
		
		//根据sql创建SQLQuery
		SQLQuery query=session.createSQLQuery(sql).addEntity(CourseEntity.class);
		query.setString(0, courseEntity.getSelect_enable())
			 .setString(1, courseEntity.getCourseNo());

		//执行新增
		int r=query.executeUpdate();
		if(r>0){
			System.out.println("新增成功");
		}else{
			System.out.println("新增失败");
		}
		transaction.commit();
		session.close();
		sessionFactory.close();
		
		return true;
	}
	
	public FileEntity getFileEntityByfileName(String fileName) {
		FileEntity fileEntity = new FileEntity();
		// TODO: 通过传入的文件名称找到file数据库中对应的项，并赋值给fileEntity,最后返回fileEntity
		//Return : fileEntity
		
		
		WebApplicationContext ctx=ContextLoader.getCurrentWebApplicationContext();
		//ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		String str;
		FileDaoImpl fileDao = (FileDaoImpl)ctx.getBean("fileDao");
		List <FileEntity> file=new ArrayList<FileEntity>();
		
		str="and fileName="+"'"+fileName+"'";
		file=fileDao.findByCondition(str,null,null,true);
		
		fileEntity.setFileLocation(file.get(0).getFileLocation());
		fileEntity.setFileName(file.get(0).getFileName());
		fileEntity.setFileType(file.get(0).getFileType());
		fileEntity.setReleaseTime(file.get(0).getReleaseTime());
		
		return fileEntity;
	}
	
	public boolean deleteFileEntity(FileEntity fileEntity) {
		// TODO: 将传入的fileEntity在file表中删除
		//Return : true | false
		
		WebApplicationContext ctx=ContextLoader.getCurrentWebApplicationContext();
		//ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");

		FileDaoImpl fileDao = (FileDaoImpl)ctx.getBean("fileDao");
		
		fileDao.deleteByIds(fileEntity.getFileName());
		
		return true;
	}
	
	public majorEntity getMajorEntityByMajorNo(String majorNo) {
		majorEntity majorentity = new majorEntity();
		// TODO: 通过传入的专业代码称找到major数据库中对应的项，并赋值给majorentity,最后返回majorentity
		//Return : majorentity
		
		WebApplicationContext ctx=ContextLoader.getCurrentWebApplicationContext();
		//ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		String str;
		MajorDaoImpl majorDao = (MajorDaoImpl)ctx.getBean("majorDao");
		List <majorEntity> major=new ArrayList<majorEntity>();
		
		str="and majorNo="+"'"+majorNo+"'";
		major=majorDao.findByCondition(str,null,null,true);
		
		majorentity.setDepartmentNo(major.get(0).getDepartmentNo());
		majorentity.setMajorName(major.get(0).getMajorName());
		majorentity.setMajorNo(major.get(0).getMajorNo());
		
		return majorentity;
	}
	
	public boolean deleteMajorEntity(majorEntity majorentity) {
		// TODO: 将传入的majorentity在major表中删除
		//Return : true | false
		
		WebApplicationContext ctx=ContextLoader.getCurrentWebApplicationContext();
		//ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");

		MajorDaoImpl majorDao = (MajorDaoImpl)ctx.getBean("majorDao");
		
		majorDao.deleteByIds(majorentity.getMajorNo());
		
		return true;
	}
	
	public boolean addMajorEntity(majorEntity majorentity) {
		// TODO: 通过传入的majorentity添加到major表中
		//Return : true | false
		
		WebApplicationContext ctx=ContextLoader.getCurrentWebApplicationContext();
		//ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		MajorDaoImpl majorDao = (MajorDaoImpl)ctx.getBean("majorDao");
		majorDao.save(majorentity);
		
		return true;
	}
	
	public boolean addDepartmentEntity(DepartmentEntity departmentEntity) {
		// TODO: 通过传入的departmentEntity添加到department表中
		//Return : true | false
		
		WebApplicationContext ctx=ContextLoader.getCurrentWebApplicationContext();
		//ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		DepartmentDaoImpl departmentDao = (DepartmentDaoImpl)ctx.getBean("departmentDao");
		departmentDao.save(departmentEntity);
		
		return true;
	}
	
	public StudentEntity getStudentEntityByStudentNo(String studentNo) {
		StudentEntity studentEntity = new StudentEntity();
		// TODO: 通过传入的学生代码称找到student数据库中对应的项，并赋值给studentEntity,最后返回studentEntity
		//Return : studentEntity
		
		WebApplicationContext ctx=ContextLoader.getCurrentWebApplicationContext();
		//ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		String str;
		StudentDaoImpl studentDao = (StudentDaoImpl)ctx.getBean("studentDao");
		List <StudentEntity> student=new ArrayList<StudentEntity>();
		
		str="and studentNo="+"'"+studentNo+"'";
		student=studentDao.findByCondition(str,null,null,true);
		
		studentEntity.setStudentNo(student.get(0).getStudentNo());
		studentEntity.setStudentName(student.get(0).getStudentName());
		studentEntity.setStudentMajor(student.get(0).getStudentMajor());
		studentEntity.setStudentGrade(student.get(0).getStudentGrade());
		studentEntity.setStudentDepartment(student.get(0).getStudentDepartment());
		
		return studentEntity;
	}
	
	public boolean deleteStudentEntity(StudentEntity studentEntity) {
		// TODO: 将传入的studentEntity从表中删除 
		//Return : true | false
		
		WebApplicationContext ctx=ContextLoader.getCurrentWebApplicationContext();
		//ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");

		StudentDaoImpl studentDao = (StudentDaoImpl)ctx.getBean("studentDao");
		
		studentDao.deleteByIds(studentEntity.getStudentNo());
		
		return true;
	}
	
	public boolean addStudentEntity(StudentEntity studentEntity) {
		// TODO: 通过传入的studentEntity添加到student表中
		//Return : true | false
		
		WebApplicationContext ctx=ContextLoader.getCurrentWebApplicationContext();
		//ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		StudentDaoImpl studentDao = (StudentDaoImpl)ctx.getBean("studentDao");
		studentDao.save(studentEntity);
		
		return true;
	}
	
	public boolean addAccountEntity(AccountEntity accountEntity) {
		// TODO: 通过传入的studentEntity添加到student表中
		//Return : true | false
		
		WebApplicationContext ctx=ContextLoader.getCurrentWebApplicationContext();
		//ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		AccountDaoImpl accountDao = (AccountDaoImpl)ctx.getBean("accountDao");
		accountDao.save(accountEntity);
		
		return true;
	}
	
	public TeacherEntity getTeacherEntityByTeacherNo(String teacherNo) {
		TeacherEntity teacherEntity = new TeacherEntity();
		// TODO: 通过传入的教师代码称找到teacher数据库中对应的项，并赋值给teacherEntity,最后返回teacherEntity
		//Return : studentEntity
		
		WebApplicationContext ctx=ContextLoader.getCurrentWebApplicationContext();
		//ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		String str;
		TeacherDaoImpl teacherDao = (TeacherDaoImpl)ctx.getBean("teacherDao");
		List <TeacherEntity> teacher=new ArrayList<TeacherEntity>();
		
		str="and teacherNo="+"'"+teacherNo+"'";
		teacher=teacherDao.findByCondition(str,null,null,true);
		
		teacherEntity.setTeacherNo(teacher.get(0).getTeacherNo());
		teacherEntity.setTeacherName(teacher.get(0).getTeacherName());
		teacherEntity.setTeacherNo(teacher.get(0).getTeacherNo());
		
		return teacherEntity;
	}
	
	public boolean deleteTeacherEntity(TeacherEntity teacherEntity) {
		// TODO: 将传入的teacherEntity从表中删除 
		//Return : true | false
		
		WebApplicationContext ctx=ContextLoader.getCurrentWebApplicationContext();
		//ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");

		TeacherDaoImpl teacherDao = (TeacherDaoImpl)ctx.getBean("teacherDao");
		
		teacherDao.deleteByIds(teacherEntity.getTeacherNo());
		
		return true;
	}
	
	public boolean addTeacherEntity(TeacherEntity teacherEntity) {
		// TODO: 通过传入的teacherEntity添加到teacher表中
		//Return : true | false
		WebApplicationContext ctx=ContextLoader.getCurrentWebApplicationContext();
		//ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		TeacherDaoImpl teacherDao = (TeacherDaoImpl)ctx.getBean("teacherDao");
		teacherDao.save(teacherEntity);
		
		return true;
	}
	
	public CourseEntity getCourseEntityByCourseNo(String courseNo) {
		CourseEntity courseEntity = new CourseEntity();
		// TODO: 通过传入的课程代码称找到course数据库中对应的项，并赋值给courseEntity,最后返回courseEntity
		//Return : courseEntity
		
		WebApplicationContext ctx=ContextLoader.getCurrentWebApplicationContext();
		//ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		String str;
		CourseDaoImpl courseDao = (CourseDaoImpl)ctx.getBean("courseDao");
		List <CourseEntity> course=new ArrayList<CourseEntity>();
		
		str="and courseNo="+"'"+courseNo+"'";
		course=courseDao.findByCondition(str,null,null,true);
		
		courseEntity.setCourseInfo(course.get(0).getCourseInfo());
		courseEntity.setCourseName(course.get(0).getCourseName());
		courseEntity.setCourseNo(course.get(0).getCourseNo());
		courseEntity.setIndex(course.get(0).getIndex());
		courseEntity.setMajorNo(course.get(0).getMajorNo());
		courseEntity.setSelect_enable(course.get(0).getSelect_enable());
		courseEntity.setTeacherNo(course.get(0).getTeacherNo());
		
		return courseEntity;
	}
	
	public boolean deleteCourseEntity(CourseEntity courseEntity) {
		// TODO: 将传入的courseEntity从表中删除 
		//Return : true | false
		WebApplicationContext ctx=ContextLoader.getCurrentWebApplicationContext();
		//ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");

		CourseDaoImpl courseDao = (CourseDaoImpl)ctx.getBean("courseDao");
		
		courseDao.deleteByIds(courseEntity.getCourseNo());
		return true;
	}
	
	public boolean addCourseEntity(CourseEntity courseEntity) {
		// TODO: 通过传入的courseEntity添加到course表中
		//Return : true | false
		WebApplicationContext ctx=ContextLoader.getCurrentWebApplicationContext();
//		//ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		SessionFactory sessionFactory = (SessionFactory)ctx.getBean("sessionFactory");
		Session session=sessionFactory.openSession();
		Transaction transaction=session.getTransaction();
		transaction.begin();
		String sql="INSERT INTO `javaEE_lab_database`.`courseTable` (`courseNo`, `index`, `courseName`, `teacherNo`, `enableSelect`, `majorNo`, `courseInfo`) VALUES (?,?, ?, ?, ?, ?, ?);";
		
		//根据sql创建SQLQuery
		SQLQuery query=session.createSQLQuery(sql).addEntity(CourseEntity.class);
		query.setString(0, courseEntity.getCourseNo())
			 .setString(1, courseEntity.getIndex())
			 .setString(2, courseEntity.getCourseName())
			 .setString(3, courseEntity.getTeacherNo())
			 .setString(4, courseEntity.getSelect_enable())
			 .setString(5, courseEntity.getMajorNo())
			 .setString(6, courseEntity.getCourseInfo() );

		//执行新增
		int r=query.executeUpdate();
		if(r>0){
			System.out.println("新增成功");
		}else{
			System.out.println("新增失败");
		}
		transaction.commit();
		session.close();
		sessionFactory.close();
		
		return true;
	}
	
	
	public List<PyqEntity> getPyqEntities(){
		List<PyqEntity> pyqEntities = new ArrayList<PyqEntity>();
		
		return pyqEntities;
	}
	
	public boolean updateStudent(StudentEntity studentEntity) {
		// TODO: 通过传入的courseEntity添加到course表中
		//Return : true | false
		//WebApplicationContext ctx=ContextLoader.getCurrentWebApplicationContext();
		ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		StudentDaoImpl studentDaoImpl = (StudentDaoImpl)ctx.getBean("studentDao");
		studentDaoImpl.update(studentEntity);
		return true;
	}
}
